//
//  ScanResumeViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/2/3.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "ScanResumeViewController.h"
#import "CreateResumeTableViewCell.h"
#import "PictureModel.h"
#import "AlumContentViewController.h"
@interface ScanResumeViewController ()
<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * _tableView;
    int mytag;
    UIView * tableHeaderV;
    UIView * sectionOne;
    UIView * sectionTwo;
    
    NSMutableArray * dataSource;
    
    EditResumeModel * tempmodel;
}

@end

@implementation ScanResumeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_isDidselected) {
        [self questData];
    }
    else
    {
        [self main];
    }
    
    self.navigationItem.title = @"简历预览";
   // [self addBackItem];
   // [self addNavgationBar];
    // Do any additional setup after loading the view.
}
-(void)questData
{
    __weak typeof(self)weakSelf = self;
    [U76HONet editResumeWithAction:@"" code:[Singleton share].code rid:_rid andDataBlock:^(EditResumeModel *model) {
        tempmodel = model;
        NSLog(@"编辑简历获取数据成功列");
        
//        UITextField * titleField = (UITextField *)[weakSelf.view viewWithTag:1];
//        titleField.text = model.title;
//        _addResumeModel.title = model.title;
//        
//        UITextField * realNameField = (UITextField *)[weakSelf.view viewWithTag:2];
//        realNameField.text = model.realname;
//        _addResumeModel.realname = model.realname;
//        
//        UITextField * sexField = (UITextField *)[weakSelf.view viewWithTag:3];
//        if ([model.sex isEqualToString:@"1"]) {
//            sexField.text = @"男";
//        }
//        else if ([model.sex isEqualToString:@"2"])
//        {
//            sexField.text = @"女";
//        }
//        _addResumeModel.sex = model.sex;
//        
//        UITextField * telFied = (UITextField *)[weakSelf.view viewWithTag:4];
//        telFied.text = model.tel;
//        _addResumeModel.tel = model.tel;
//        
//        UITextField * workYearField = (UITextField *)[weakSelf.view viewWithTag:5];
//        workYearField.text = model.years;
//        _addResumeModel.workyear = model.workyear;
//        
//        UITextField * birthdayField = (UITextField *)[weakSelf.view viewWithTag:6];
//        NSString * str = model.birthday;
//        NSMutableArray * mArr = [[NSMutableArray alloc]init];
//        
//        NSString * sub1 = [str substringToIndex:4];
//        [mArr addObject:sub1];
//        NSString * sub2 = [str substringWithRange:NSMakeRange(4, 2)];
//        [mArr addObject:sub2];
//        NSString * sub3 = [str substringWithRange:NSMakeRange(6, 2)];
//        [mArr addObject:sub3];
//        
//        NSString * birthday = [mArr componentsJoinedByString:@"-"];
//        birthdayField.text = birthday;
//        _addResumeModel.birthday = model.birthday;
//        
//        UITextField * noaddrField = (UITextField *)[weakSelf.view viewWithTag:7];
//        noaddrField.text = model.nowaddrname;
//        _addResumeModel.nowaddr = model.nowaddr;
//        
//        UITextField * workCityField = (UITextField *)[weakSelf.view viewWithTag:8];
//        workCityField.text = model.workcityname;
//        _addResumeModel.workcity = model.workplace;
//        
//        UITextField * homeAddrField = (UITextField *)[weakSelf.view viewWithTag:9];
//        homeAddrField.text =model.homeaddrname;
//        _addResumeModel.homeaddr = model.homeaddr;
//        
//        UITextField * dutyField = (UITextField *)[weakSelf.view viewWithTag:10];
//        dutyField.text = model.dutyname;
//        _addResumeModel.dutyid = model.dutyid;
//        
//        UITextField * salaryField = (UITextField *)[weakSelf.view viewWithTag:11];
//        salaryField.text = model.salaryname;
//        _addResumeModel.salary = model.salary;
//        
//        UITextView * textView = (UITextView *)[weakSelf.view viewWithTag:12];
//        textView.text = model.intro;
//        _addResumeModel.intro = model.intro;
//        
//        _addResumeModel.ispic = model.ispic;
//        
//        _addResumeModel.mypics = model.mypics;
//        if ([_addResumeModel.ispic isEqualToString:@"0"]) {
//            
//        }
//        else
//        {
//            UIButton * saveBtn = (UIButton *)[self.view viewWithTag:513];
//            saveBtn.selected = YES;
//        }
//        for (int i = 0; i< 2; i++) {
//            UILabel * lab = (UILabel *)[self.view viewWithTag:i+20];
//            lab.hidden = YES;
//        }
        [weakSelf main];
    }];
}
-(void)main
{
    dataSource = [[NSMutableArray alloc]init];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [self createTableFooterView];
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = UIColorFromRGB(0xf1f3f2);
    [_tableView registerNib:[UINib nibWithNibName:@"CreateResumeTableViewCell" bundle:nil] forCellReuseIdentifier:@"CreateResumeTableViewCell"];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.backgroundColor = UIColorFromRGB(0xf1f3f2);
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    if ([_addResumeModel.ispic isEqualToString:@"0"]) {
        
    }
    else
    {
        if (_isDidselected) {
            if ([tempmodel.mypics isEqualToString:@""]) {
                
            }
            else
            {
                [self alumData2];
            }
        }
        else
        {
            if ([_addResumeModel.mypics isEqualToString:@""]) {
                
            }
            else
            {
                [self alumData];
            }
        }
    }
}
-(void)alumData
{
    __weak typeof(self)weakSelf = self;
    [U76HONet minePictureWithaction:@"" code:[Singleton share].code andDataBlock:^(NSArray *arr) {
        NSLog(@"预览相册获取我的图片库数据成功了");
        NSRange range2 = [_addResumeModel.mypics rangeOfString:@"|"];
        if (range2.location != NSNotFound) {
            NSArray * array = [_addResumeModel.mypics componentsSeparatedByString:@"|"];
            for (int i = 0; i<arr.count; i++) {
                PictureModel * model = arr[i];
                for (int j = 0;j < array.count; j++) {
                    NSString * str = array[j];
                    if ([model.ids isEqualToString:str]) {
                        [dataSource addObject:model];
                    }
                }
            }
        }
        else
        {
            for (int i = 0; i<arr.count; i++) {
                PictureModel * model = arr[i];
                if ([_addResumeModel.mypics isEqualToString:model.ids]) {
                    [dataSource addObject:model];
                }
            }
        }
        _tableView.tableFooterView = [weakSelf createTableFooterView];
    }];

}
-(void)alumData2
{
    __weak typeof(self)weakSelf = self;
    [U76HONet minePictureWithaction:@"" code:[Singleton share].code andDataBlock:^(NSArray *arr) {
        NSLog(@"预览相册获取我的图片库数据成功了");
        NSRange range = [tempmodel.mypics rangeOfString:@"|"];
        if (range.location != NSNotFound) {
            NSArray * array = [tempmodel.mypics componentsSeparatedByString:@"|"];
            for (int i = 0; i<arr.count; i++) {
                PictureModel * model = arr[i];
                for (int j = 0;j < array.count; j++) {
                    NSString * str = array[j];
                    if ([model.ids isEqualToString:str]) {
                        [dataSource addObject:model];
                    }
                }
            }
        }
        else
        {
            for (int i = 0; i<arr.count; i++) {
                PictureModel * model = arr[i];
                if ([tempmodel.mypics isEqualToString:model.ids]) {
                    [dataSource addObject:model];
                }
            }
        }
        _tableView.tableFooterView = [weakSelf createTableFooterView];
    }];
}
#pragma mark - _tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CreateResumeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CreateResumeTableViewCell"];
    NSString * string1 = nil;
    if (indexPath.section == 0) {
        string1 = @"简历名称";
        cell.isHiddenSecLab = @"YES";
    }
    else if (indexPath.section == 1) {
        string1 = @"基本信息 ";
    }
    else if(indexPath.section == 2)
    {
        string1 = @"个人简历 ";
    }
    cell.backgroundColor = UIColorFromRGB(0xf1f3f2);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.firstLab.text = string1;
    
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return [self createTabelHeaderView];
    }
    else if (section == 1) {
        return [self createSection0View];
    }
    else
    {
        return [self createSection1View];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 50;
    }
    else if (section == 1) {
        return 400;
    }
    else
    {
        if (_isDidselected) {
            return 15*2+[tempmodel.intro heightOfSize:17 width:SCREEN_WIDTH-30];
        }
        return 15*2+[_addResumeModel.intro heightOfSize:17 width:SCREEN_WIDTH-30];
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

#pragma mark - createHeaderView
-(UIView *)createTabelHeaderView
{
    tableHeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    tableHeaderV.backgroundColor = UIColorFromRGB(0xf1f3f2);
    UITextField * resumeName = [[UITextField alloc]initWithFrame:CGRectMake(SIZETO6W(20), 0, SCREEN_WIDTH-SIZETO6W(15)*2, 50) ];
    //    resumeName.layer.masksToBounds = YES;
    //    resumeName.layer.borderWidth = 1;
    resumeName.tag = 1;
    resumeName.enabled = NO;
    resumeName.layer.borderColor = [[UIColor blackColor] CGColor];
    resumeName.backgroundColor = [UIColor whiteColor];
    [resumeName setValue:UIColorFromRGB(0xbbbfc3) forKeyPath:@"_placeholderLabel.textColor"];
    if (_isDidselected) {
        resumeName.text = tempmodel.title;
    }
    else
    {
        resumeName.text = _addResumeModel.title;
    }
    [tableHeaderV addSubview:resumeName];
    
    return tableHeaderV;
}
-(UIView *)createTableFooterView
{
    CGFloat alumWidth = (SCREEN_WIDTH - SIZETO6W(15)*2 - SIZETO6W(10)*2)/3;
    int j = 0;
    for (int i = 0; i<dataSource.count; i++) {
        int row=i%3;//行号
        
        if (row == 0) {
            j++;
        }
    }
    NSLog(@"*******************%i",(int)j);
    UIView * tableFooterV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, (SIZETO6W(15)*2 + (j-1)*(alumWidth + SIZETO6W(10) ) + alumWidth))];
    tableFooterV.userInteractionEnabled = YES;
    for (int i = 0; i<dataSource.count; i++) {
        PictureModel * model = dataSource[i];
        int row=i/3;//行号
        int loc=i%3;//列号
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        
        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15) + (alumWidth + SIZETO6W(10)) * loc, SIZETO6W(10) + (alumWidth + SIZETO6W(10))*row, alumWidth, alumWidth)];
        imgV.tag = i+1;
        NSRange range = [model.pic rangeOfString:@"|"];
        if (range.location != NSNotFound) {
            NSArray * arr = [model.pic componentsSeparatedByString:@"|"];
            [imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,arr.lastObject]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
            
        }
        else
        {
            [imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,model.pic]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
        }
        imgV.userInteractionEnabled = YES;
        [imgV addGestureRecognizer:tap];
        [tableFooterV addSubview:imgV];
    }
    
    
    return tableFooterV;
}
-(UIView *)createSection0View
{
        sectionOne = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 400)];
        UIView * subV = [[UIView alloc]initWithFrame:CGRectMake(15, 5, SCREEN_WIDTH-30, 400)];
        subV.backgroundColor = [UIColor whiteColor];
        //    subV.layer.masksToBounds = YES;
        //    subV.layer.borderWidth = 0.5;
        //    subV.layer.borderColor = [[UIColor blackColor]CGColor];
        [sectionOne addSubview:subV];
        NSMutableArray * arr = [[NSMutableArray alloc]init];
        [arr addObjectsFromArray:@[@"姓       名:",@"性       别:",@"联系电话:",@"工作年限:",@"出生日期:",@"现居住地:",@"想工作地:",@"籍       贯:",@"职位类别:",@"期望薪资:"]];
        for (int i = 0; i<arr.count;i++) {
            
            UIView * lastSubV = [[UIView alloc]initWithFrame:CGRectMake(0,i*40 , SCREEN_WIDTH, 40)];
            lastSubV.userInteractionEnabled = YES;
            lastSubV.tag = 100+i;
            [subV addSubview:lastSubV];
            
            UILabel * nameLab = [[UILabel alloc]initWithFrame:CGRectMake(10, i*40, 90, 40)];
            nameLab.text = arr[i];
            [subV addSubview:nameLab];
            subV.userInteractionEnabled = YES;
            UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(nameLab.right, nameLab.top, subV.frame.size.width-nameLab.right-50, 40)];
            textField.tag = i+2;
            textField.enabled = NO;
            if (i == 0) {
                if (_isDidselected) {
                    textField.text = tempmodel.realname;
                }
                else
                {
                    textField.text = _addResumeModel.realname;
                }
            }
            else if (i == 1)
            {
                if (_isDidselected) {
                    if ([tempmodel.sex isEqualToString:@"1"]) {
                        textField.text = @"男";
                    }
                    else
                    {
                        textField.text = @"女";
                    }
                }
                else
                {
                    textField.text = _addResumeModel.sex;
                }

            }
            else if (i == 2)
            {
                if (_isDidselected) {
                    textField.text = tempmodel.tel;
                }
                else
                {
                    textField.text = _addResumeModel.tel;
                }

            }
            else if (i == 3)
            {
                if (_isDidselected) {
                    textField.text = tempmodel.years;
                }
                else
                {
                    textField.text = _addResumeModel.workyear;
                }

            }
            else if (i == 4)
            {
                if (_isDidselected) {
                    textField.text = tempmodel.birthday;
                }
                else
                {
                    textField.text = _addResumeModel.birthday;
                }

            }
            else if (i == 5)
            {
                if (_isDidselected) {
                    textField.text = tempmodel.nowaddrname;
                }
                else
                {
                    textField.text = _addResumeModel.nowaddr;
                }

            }
            else if (i == 6)
            {
                if (_isDidselected) {
                    textField.text = tempmodel.workcityname;
                }
                else
                {
                    textField.text = _addResumeModel.workcity;
                }

            }
            else if (i == 7)
            {
                if (_isDidselected) {
                    textField.text = tempmodel.homeaddrname;
                }
                else
                {
                    textField.text = _addResumeModel.homeaddr;
                    
                }
                

            }
            else if (i == 8)
            {
                if (_isDidselected) {
                    textField.text = tempmodel.dutyname;
                }
                else
                {
                    textField.text = _addResumeModel.dutyid;
                }
            }
            else
            {
                if (_isDidselected) {
                    textField.text = tempmodel.salaryname;
                }
                else
                {
                    
                    textField.text = _addResumeModel.salary;
                }
                
            }
            textField.textAlignment = NSTextAlignmentLeft;
            [subV addSubview:textField];
            
    }
    
    

    
    return sectionOne;
}
-(UIView *)createSection1View
{
    if (_isDidselected) {
        sectionTwo = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15*2+[tempmodel.intro heightOfSize:17 width:SCREEN_WIDTH-30])];
        UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH-30, [tempmodel.intro heightOfSize:17 width:SCREEN_WIDTH-30])];
        lab.numberOfLines = 0;
        lab.textColor = UIColorFromRGB(0x333944);
        lab.font = [UIFont systemFontOfSize:15];
        lab.text = tempmodel.intro;
        [sectionTwo addSubview:lab];
        sectionTwo.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        sectionTwo = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15*2+[_addResumeModel.intro heightOfSize:17 width:SCREEN_WIDTH-30])];
        UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, SCREEN_WIDTH-30, [_addResumeModel.intro heightOfSize:17 width:SCREEN_WIDTH-30])];
        lab.numberOfLines = 0;
        lab.textColor = UIColorFromRGB(0x333944);
        lab.font = [UIFont systemFontOfSize:15];
        lab.text = _addResumeModel.intro;
        [sectionTwo addSubview:lab];
        sectionTwo.backgroundColor = [UIColor whiteColor];
    }
    return sectionTwo;
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    int i = (int)tap.view.tag - 1;
    NSLog(@"************%i",i);
    PictureModel * model = dataSource[i];
    AlumContentViewController * alumContentVC = [[AlumContentViewController alloc]init];
    alumContentVC.alumId = model.ids;
    alumContentVC.alumName = model.name;
    alumContentVC.isResumeAlum = YES;
    [self.navigationController pushViewController:alumContentVC animated:YES];
}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
